package com.project.systemfront.controller;

import com.project.systemfront.common.Result;
import com.project.systemfront.pojo.Favorite;
import com.project.systemfront.pojo.vo.FavoriteVo;
import com.project.systemfront.service.IArtworkService;
import com.project.systemfront.service.IFavoriteService;
import com.project.systemfront.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/favorite")
public class FavoriteController
{
    @Autowired
    private IFavoriteService favoriteService;

    @Autowired
    private IArtworkService artworkService;


   @GetMapping("/add")
    public Result<String> add(@RequestParam Long artworkId)
    {

        Map<String, Object> claims= ThreadLocalUtil.get();
        Long id=Long.parseLong(claims.get("id").toString());
        Favorite favorite = new Favorite();
        favorite.setArtworkId(artworkId);
        favorite.setUserId(id);
        favorite.setCreateTime(new Date());
        List<FavoriteVo> favoriteVos = favoriteService.selectFavoriteList(favorite);
            if(favoriteVos.size()>0){
                return Result.error(500,"您已经收藏过该作品");
            }
        favoriteService.insertFavorite(favorite);
        artworkService.incrementFavoriteCount(artworkId);
        return Result.success("收藏成功");
    }
    @GetMapping
    public Result<List<FavoriteVo>> getList()
    {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Long id=Long.parseLong(claims.get("id").toString());
        Favorite favorite = new Favorite();
        favorite.setUserId(id);
        List<FavoriteVo> list = favoriteService.selectFavoriteList(favorite);
        return Result.success(list);
    }

    @GetMapping("/check/{id}")
    public Result check(@PathVariable("id") Long id)
    {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Long userId=Long.parseLong(claims.get("id").toString());
        Favorite favorite = new Favorite();
        favorite.setArtworkId(id);
        favorite.setUserId(userId);
        List<FavoriteVo> favoriteVos = favoriteService.selectFavoriteList(favorite);

          if (favoriteVos.size()>0){
              return Result.success("true");
          }
          return Result.success("false");

    }
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable("id") Long id)
    {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Long userId=Long.parseLong(claims.get("id").toString());
        Favorite favorite = new Favorite();
        favorite.setArtworkId(id);
        favorite.setUserId(userId);
        List<FavoriteVo> favoriteVos = favoriteService.selectFavoriteList(favorite);
        if(favoriteVos.size()>0){
            favoriteService.deleteFavoriteById(favoriteVos.get(0).getId());
            artworkService.decrementFavoriteCount(id);
            return Result.success("删除成功");
        }
        return Result.error(500,"您没有收藏过该作品");


    }
}
